package com.tc.quartz.frame.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;

/**
 * PaymentQuartz
 * @author yongjin.ye
 */
public class MD5 {
	
	protected static Logger log = LoggerFactory.getLogger(MD5.class);
	
	private static char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static String encode(String s, String salt) {
		if (s == null) return null;
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(salt.getBytes());
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexChars[byte0 >>> 4 & 0xf];
				str[k++] = hexChars[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			log.error("MD5加密异常", e);
			return null;
		}
	}
}
